package com.jcl.recruit.console.report.logic;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.elasticsearch.resume.dao.ConsoleReportSearchResumeDao;
import com.jcl.recruit.elasticsearch.resume.vo.ElasticReportCondition;
import org.apache.lucene.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ConsoleReportLogic
 * @Author yyliu
 * @Date 2018/12/25 14:53
 * @Version 1.0
 **/
@Component
public class ConsoleReportLogic {

    @Autowired
    private ConsoleReportSearchResumeDao consoleReportSearchResumeDao;

    public List<Map<String, Object>> pieChar(ElasticReportCondition condition) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Map<String, Object>> dataList = consoleReportSearchResumeDao.pieChar(condition);
        if(CollectionUtils.isNotEmpty(dataList)){
            for(Map<String, Object> map: dataList){
                Map<String, Object> data = new HashMap<>();
                data.put("name", map.get("source"));
                data.put("value", map.get("number"));
                resultList.addAll(dataList);
            }
        }
        return resultList;
    }


    public Map<String, Object> barGraph(ElasticReportCondition condition) {
        Map<String, Object> dataMap = new HashMap<>();
        List<String> y = DateUtils.dateSection(condition.getStartDate(), condition.getEndDate(), true);
        List<Map<String, Object>> x = new ArrayList<>();
        List<Map<String, Object>> dataList = consoleReportSearchResumeDao.barGraph(condition);
        if(CollectionUtils.isNotEmpty(dataList)){
            Map<String, Map<String, String>> sourceMap = new HashMap<>();
            for(Map<String, Object> map: dataList){
                Map<String, String> sourceData = sourceMap.get(map.get("source"));
                if(null == sourceData){
                    sourceData = new HashMap<>();
                    sourceMap.put(map.get("source").toString(), sourceData);
                }
                sourceData.put(map.get("days").toString(), map.get("number").toString());
            }
            for(Map.Entry<String, Map<String, String>> entry: sourceMap.entrySet()){
                Map<String, Object> xData = new HashMap<>();
                List<Integer> numberList = new ArrayList<>();
                for(String day: y){
                    String temp = entry.getValue().get(day);
                    if(null == temp){
                        numberList.add(0);
                    }else{
                        numberList.add(Integer.valueOf(temp));
                    }
                }
                xData.put("name", entry.getKey());
                xData.put("value", numberList);
                x.add(xData);
            }
        }
        dataMap.put("y", y);
        dataMap.put("x", x);
        return dataMap;
    }
}
